package cn.xkai.exercise.a;

/**
 * @description: 长度最小的子数组
 * 自己的思路：未完成
 * 借鉴的思路：双指针滑动窗口
 * 心得：认值读题，连续且和大于等于目标值
 * @author: kaixiang
 * @date: 2022-06-27
 **/
public class Solution17 {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0, right = 0, sum = 0, min = Integer.MAX_VALUE;
        while (right < nums.length) {
            sum += nums[right];
            while (sum >= target) {
                min = Math.min(min, right - left + 1);
                sum -= nums[left];
                left++;
            }
            right++;
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }


    public static void main(String[] args) {
        int[] nums = {2, 3, 1, 2, 3, 5};
        int target = 7;
        Solution17 solution17 = new Solution17();
        System.out.println(solution17.minSubArrayLen(target, nums));
    }
}
